1   /*
2    * Copyright (C) 2008 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.collect.testing.testers;
18  
19  import static com.google.common.collect.testing.features.CollectionFeature.ALLOWS_NULL_VALUES;
20  import static com.google.common.collect.testing.features.CollectionFeature.FAILS_FAST_ON_CONCURRENT_MODIFICATION;
21  import static com.google.common.collect.testing.features.CollectionSize.ONE;
22  import static com.google.common.collect.testing.features.CollectionSize.ZERO;
23  import static com.google.common.collect.testing.features.ListFeature.SUPPORTS_ADD_WITH_INDEX;
24  
25  import com.google.common.annotations.GwtCompatible;
26  import com.google.common.annotations.GwtIncompatible;
27  import com.google.common.collect.testing.Helpers;
28  import com.google.common.collect.testing.features.CollectionFeature;
29  import com.google.common.collect.testing.features.CollectionSize;
30  import com.google.common.collect.testing.features.ListFeature;
31  
32  import java.lang.reflect.Method;
33  import java.util.ConcurrentModificationException;
34  import java.util.Iterator;
35  
36  /**
37   * A generic JUnit test which tests {@code add(int, Object)} operations on a
38   * list. Can't be invoked directly; please see
39   * {@link com.google.common.collect.testing.ListTestSuiteBuilder}.
40   *
41   * @author Chris Povirk
42   */
43  @SuppressWarnings("unchecked") // too many "unchecked generic array creations"
44  @GwtCompatible(emulated = true)
45  public class ListAddAtIndexTester<E> extends AbstractListTester<E> {
46    @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX)
47    @CollectionSize.Require(absent = ZERO)
48    public void testAddAtIndex_supportedPresent() {
49      getList().add(0, samples.e0);
50      expectAdded(0, samples.e0);
51    }
52  
53    @ListFeature.Require(absent = SUPPORTS_ADD_WITH_INDEX)
54    @CollectionSize.Require(absent = ZERO)
55    /*
56     * absent = ZERO isn't required, since unmodList.add() must
57     * throw regardless, but it keeps the method name accurate.
58     */
59    public void testAddAtIndex_unsupportedPresent() {
60      try {
61        getList().add(0, samples.e0);
62        fail("add(n, present) should throw");
63      } catch (UnsupportedOperationException expected) {
64      }
65      expectUnchanged();
66    }
67  
68    @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX)
69    public void testAddAtIndex_supportedNotPresent() {
70      getList().add(0, samples.e3);
71      expectAdded(0, samples.e3);
72    }
73  
74    @CollectionFeature.Require(FAILS_FAST_ON_CONCURRENT_MODIFICATION)
75    @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX)
76    public void testAddAtIndexConcurrentWithIteration() {
77      try {
78        Iterator<E> iterator = collection.iterator();
79        getList().add(0, samples.e3);
80        iterator.next();
81        fail("Expected ConcurrentModificationException");
82      } catch (ConcurrentModificationException expected) {
83        // success
84      }
85    }
86  
87    @ListFeature.Require(absent = SUPPORTS_ADD_WITH_INDEX)
88    public void testAddAtIndex_unsupportedNotPresent() {
89      try {
90        getList().add(0, samples.e3);
91        fail("add(n, notPresent) should throw");
92      } catch (UnsupportedOperationException expected) {
93      }
94      expectUnchanged();
95      expectMissing(samples.e3);
96    }
97  
98    @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX)
99    @CollectionSize.Require(absent = {ZERO, ONE})
100   public void testAddAtIndex_middle() {
101     getList().add(getNumElements() / 2, samples.e3);
102     expectAdded(getNumElements() / 2, samples.e3);
103   }
104 
105   @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX)
106   @CollectionSize.Require(absent = ZERO)
107   public void testAddAtIndex_end() {
108     getList().add(getNumElements(), samples.e3);
109     expectAdded(getNumElements(), samples.e3);
110   }
111 
112   @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX)
113   @CollectionFeature.Require(ALLOWS_NULL_VALUES)
114   public void testAddAtIndex_nullSupported() {
115     getList().add(0, null);
116     expectAdded(0, (E) null);
117   }
118 
119   @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX)
120   @CollectionFeature.Require(absent = ALLOWS_NULL_VALUES)
121   public void testAddAtIndex_nullUnsupported() {
122     try {
123       getList().add(0, null);
124       fail("add(n, null) should throw");
125     } catch (NullPointerException expected) {
126     }
127     expectUnchanged();
128     expectNullMissingWhenNullUnsupported(
129         "Should not contain null after unsupported add(n, null)");
130   }
131 
132   @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX)
133   public void testAddAtIndex_negative() {
134     try {
135       getList().add(-1, samples.e3);
136       fail("add(-1, e) should throw");
137     } catch (IndexOutOfBoundsException expected) {
138     }
139     expectUnchanged();
140     expectMissing(samples.e3);
141   }
142 
143   @ListFeature.Require(SUPPORTS_ADD_WITH_INDEX)
144   public void testAddAtIndex_tooLarge() {
145     try {
146       getList().add(getNumElements() + 1, samples.e3);
147       fail("add(size + 1, e) should throw");
148     } catch (IndexOutOfBoundsException expected) {
149     }
150     expectUnchanged();
151     expectMissing(samples.e3);
152   }
153 
154   /**
155    * Returns the {@link Method} instance for
156    * {@link #testAddAtIndex_nullSupported()} so that tests can suppress it. See
157    * {@link CollectionAddTester#getAddNullSupportedMethod()} for details.
158    */
159   @GwtIncompatible("reflection")
160   public static Method getAddNullSupportedMethod() {
161     return Helpers.getMethod(
162         ListAddAtIndexTester.class, "testAddAtIndex_nullSupported");
163   }
164 }